package org.wishwork.api;

import cn.hutool.core.util.StrUtil;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import org.wishwork.exception.ExceptionEnum;
import org.wishwork.exception.JSTException;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

/**
 * @description: 基础DTO
 * @author: WangYX
 * @create: 2022-02-15 11:18
 * @Version: 1.0.0
 **/
@EqualsAndHashCode
@Getter
@ToString
public abstract class ExtendBase extends Base {

    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * 修改开始时间
     */
    protected String modifiedBegin;

    /**
     * 修改结束时间
     */
    protected String modifiedEnd;

    protected ExtendBase(Integer pageIndex, Integer pageSize, String modifiedBegin, String modifiedEnd) {
        super(pageIndex, pageSize);
        this.modifiedBegin = modifiedBegin;
        this.modifiedEnd = modifiedEnd;
    }

    protected static abstract class Builder<T extends Builder, R extends ExtendBase> extends Base.Builder {

        protected String modifiedBegin;
        protected String modifiedEnd;

        public abstract T modifiedBegin(String modifiedBegin);

        public abstract T modifiedEnd(String modifiedEnd);

        @Override
        public abstract T pageIndex(Integer pageIndex);

        @Override
        public abstract T pageSize(Integer pageSize);

        @Override
        public abstract R build();

        /**
         * 关键属性为空时的时间处理
         *
         * @param days 开始时间和结束时间的时间间隔（单位：天）
         */
        public void keyIsEmptyTimeHandle(int days) {
            //两者都为空
            beginAndEndAllEmpty(days);
        }

        /**
         * 时间处理
         *
         * @param days
         */
        public void modifiedTimeHandle(int days) {
            //两者都不为空
            beginAndEndAllIsNotEmpty(days);
            //开始时间不为空，结束时间为空
            beginIsNotEmptyAndEndIsEmpty(days);
            //开始时间为空，结束时间不为空
            beginIsEmptyAndEndIsNotEmpty(days);

        }


        /**
         * 两者都为空
         *
         * @param days
         */
        private void beginAndEndAllEmpty(int days) {
            if (StrUtil.isEmpty(this.modifiedBegin) && StrUtil.isEmpty(this.modifiedEnd)) {
                LocalDateTime end = LocalDateTime.now();
                this.modifiedBegin = formatter.format(end.minusDays(days));
                this.modifiedEnd = formatter.format(end);
            }
        }

        /**
         * 开始时间不为空，结束时间为空
         */
        private void beginIsNotEmptyAndEndIsEmpty(int days) {
            if (StrUtil.isNotEmpty(this.modifiedBegin) && StrUtil.isEmpty(this.modifiedEnd)) {
                LocalDateTime begin = LocalDateTime.parse(this.modifiedBegin, formatter);
                this.modifiedEnd = formatter.format(begin.plusDays(days));
            }
        }

        /**
         * 开始时间为空，结束时间不为空
         */
        private void beginIsEmptyAndEndIsNotEmpty(int days) {
            if (StrUtil.isEmpty(this.modifiedBegin) && StrUtil.isNotEmpty(this.modifiedEnd)) {
                LocalDateTime end = LocalDateTime.parse(this.modifiedEnd, formatter);
                this.modifiedBegin = formatter.format(end.minusDays(days));
            }
        }

        /**
         * 开始时间和结束时间都不为空
         */
        private void beginAndEndAllIsNotEmpty(int days) {
            if (StrUtil.isNotEmpty(this.modifiedBegin) && StrUtil.isNotEmpty(this.modifiedEnd)) {
                LocalDateTime begin = LocalDateTime.parse(this.modifiedBegin, formatter);
                LocalDateTime end = LocalDateTime.parse(this.modifiedEnd, formatter);
                long beginSecond = begin.toEpochSecond(ZoneOffset.of("+8")) / 1000;
                long endSecond = end.toEpochSecond(ZoneOffset.of("+8")) / 1000;
                if ((endSecond - beginSecond) > days * 24 * 60 * 60) {
                    throw new JSTException(ExceptionEnum.USER_ERROR_A0420, "时间间隔不能超过" + days + "天!");
                }
            }
        }


    }
}
